"""
数据提取结合业务处理
"""

"""
"""
import logging as log


def bengzuoguan_ci(influxdb_clinet, dt_start, dt_end):
    try:
        reference = []
        reference1 = []
        values = []
        values1 = []
        result_set = list(influxdb_clinet.query("""
        SELECT value FROM "CG_LT_GL_GL04_1HTCXJZSD" WHERE time >='{}' AND  time <='{}'
        """.format(dt_start, dt_end)))
        if len(result_set) > 0:
            CG_LT_GL_GL04_1HTCXJZSD = [item['value'] for item in result_set[0]]
            reference = CG_LT_GL_GL04_1HTCXJZSD

        result_set = list(influxdb_clinet.query("""
            SELECT value FROM "CG_LT_GL_GL04_2HTCXJZSD" WHERE time >='{}' AND  time <='{}'
            """.format(dt_start, dt_end)))
        if len(result_set) > 0:
            CG_LT_GL_GL04_2HTCXJZSD = [item['value'] for item in result_set[0]]
            reference1 = CG_LT_GL_GL04_2HTCXJZSD

        result_set = list(influxdb_clinet.query("""
            SELECT value FROM "CG_LT_GL_GL04_TC01" WHERE time >='{}' AND  time <='{}'
            """.format(dt_start, dt_end)))
        if len(result_set) > 0:
            CG_LT_GL_GL04_TC01 = [item['value'] for item in result_set[0]]
            values = CG_LT_GL_GL04_TC01
        result_set = list(influxdb_clinet.query("""
            SELECT value FROM "CG_LT_GL_GL04_TC02" WHERE time >='{}' AND  time <='{}'
            """.format(dt_start, dt_end)))
        if len(result_set) > 0:
            CG_LT_GL_GL04_TC02 = [item['value'] for item in result_set[0]]
            values1 = CG_LT_GL_GL04_TC02

        print(len(reference), len(reference1), len(values), len(values1))
        T1 = catch_the_slip(values, reference)
        T2 = catch_the_slip(values1, reference1)
        num = 0
        for i in range(len(T2)):
            for j in range(len(T1)):
                if T2[i] == T1[j]:
                    num += 1
        final_num = len(T1) + len(T2) - num
        return final_num
    except Exception as e:
        log.error(e, exc_info=1)
        return 0


def catch_the_slip(v, r):
    counts = 0
    ready = 0
    start = 0
    i = 0
    a = 0
    slip = 0
    values = v
    reference = r
    T_sets = []
    #     values = v

    #     reference = []
    for i in range(len(values) - 1):

        j = i + 1
        if values[i] < values[j]:  # 下一行数据大于前一行数据
            if values[j] > ready:
                if reference[j] == 0:
                    ready = values[j]
                if reference[j] == 1:
                    if a == 0 and values[j] - values[i] > 1:
                        counts += 1
                    if a >= 1 and values[j] - values[i] > 1 and counts > 1:
                        slip += 1
                        T_sets.append(1)
                    else:
                        start = values[j]
                    a = a + 1
                    counts = 0

        # if values[i] > values[j]:  # 下一行数据小于前一行数据
        #     ready = values[j]
        #     a = 0
        #     start = 0
        # if values[i] == values[j]:  # 下一行数据等于前一行数据
        #     if values[j] < 0.1:
        #         ready = values[j]
        #     if reference[j] == 1 and values[j] != ready:
        #         start = values[j]
        #     if reference[j] == 0 and values[j] > ready:
        #         ready = values[j]
    ###即为崩料次数
    return T_sets


def dt_to_str(dt, fmt="%Y%m%d") -> str:
    return dt.strftime(fmt)


def get_start_of_day(dt):
    from datetime import datetime
    return datetime(dt.year, dt.month, dt.day)


def get_end_of_day(dt):
    from datetime import datetime, time
    return datetime.combine(dt, time.max)


if __name__ == '__main__':
    from influxdb import InfluxDBClient
    from datetime import datetime, timedelta

    for i in range(100-2,-1,-1):
        print(i)

    influxdb_client = InfluxDBClient(host="192.168.40.214", port=8086, username='admin', password='BICI123456',
                                     database='bfbd2022')



    date = datetime.now()
    day_end = get_start_of_day(date) + timedelta(hours=8)
    day_1 = get_start_of_day(date - timedelta(days=1)) + timedelta(hours=8)
    day_2 = get_start_of_day(date - timedelta(days=2)) + timedelta(hours=8)
    print(day_1, day_2, day_end)
    count = bengzuoguan_ci(influxdb_client, day_1, day_end)
    print(count)
